{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6f18a364-5d19-42bb-9da5-58ab9bea34ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nltk\n",
    "from nltk.corpus import stopwords\n",
    "from nltk.tokenize import word_tokenize, sent_tokenize\n",
    "from nltk.sentiment import SentimentIntensityAnalyzer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6c142daf-f362-4f7b-bf04-44ab5ee0aab7",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[nltk_data] Downloading package punkt to /Users/dongjin/nltk_data...\n",
      "[nltk_data]   Package punkt is already up-to-date!\n",
      "[nltk_data] Downloading package averaged_perceptron_tagger to\n",
      "[nltk_data]     /Users/dongjin/nltk_data...\n",
      "[nltk_data]   Package averaged_perceptron_tagger is already up-to-\n",
      "[nltk_data]       date!\n",
      "[nltk_data] Downloading package stopwords to\n",
      "[nltk_data]     /Users/dongjin/nltk_data...\n",
      "[nltk_data]   Package stopwords is already up-to-date!\n",
      "[nltk_data] Downloading package vader_lexicon to\n",
      "[nltk_data]     /Users/dongjin/nltk_data...\n",
      "[nltk_data]   Package vader_lexicon is already up-to-date!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 确保已下载必要的数据\n",
    "nltk.download('punkt')\n",
    "nltk.download('averaged_perceptron_tagger')\n",
    "nltk.download('stopwords')\n",
    "nltk.download('vader_lexicon')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d974cdb2-d6c3-455e-b4c2-c8cb00c33d21",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 示例文本\n",
    "text = \"Natural Language Processing (NLP) is fascinating. It is complex yet intriguing.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4460c679-4c6b-4405-984b-b90ec3b3dd78",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分句和分词\n",
    "sentences = sent_tokenize(text)\n",
    "words = word_tokenize(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "06128c50-374b-480e-998a-f57b021bf479",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Natural Language Processing (NLP) is fascinating.', 'It is complex yet intriguing.']\n"
     ]
    }
   ],
   "source": [
    "print(sentences)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a0bffbe7-04d1-4e85-9759-c9f017c84f00",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Natural', 'Language', 'Processing', '(', 'NLP', ')', 'is', 'fascinating', '.', 'It', 'is', 'complex', 'yet', 'intriguing', '.']\n"
     ]
    }
   ],
   "source": [
    "print(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "42dfd1e4-de98-403d-ab28-cd32e61df5fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 去除停用词\n",
    "stop_words = set(stopwords.words('english'))\n",
    "filter_words = [word for word in words if word.lower() not in stop_words]"
   ]
  },
  {
   "cell_type": "raw",
   "id": "9591fed0-47aa-4d04-a10a-4e30f73e48fd",
   "metadata": {},
   "source": [
    "print(stop_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e8bb2cee-d1db-456a-adb3-c06560ef5cd9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Natural', 'Language', 'Processing', '(', 'NLP', ')', 'fascinating', '.', 'complex', 'yet', 'intriguing', '.']\n"
     ]
    }
   ],
   "source": [
    "print(filter_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4e39ff56-e1b6-4ec9-b502-64042c68c5d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 词性标注\n",
    "tagged_words = nltk.pos_tag(filter_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8d5a1b0d-5ef0-4492-9c0d-a3cc0a15380f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('Natural', 'JJ'), ('Language', 'NNP'), ('Processing', 'NNP'), ('(', '('), ('NLP', 'NNP'), (')', ')'), ('fascinating', 'NN'), ('.', '.'), ('complex', 'JJ'), ('yet', 'RB'), ('intriguing', 'VBG'), ('.', '.')]\n"
     ]
    }
   ],
   "source": [
    "print(tagged_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ad15ea2d-5cd6-4a5f-84b7-1331a6c870e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Natural Language Processing (NLP) is fascinating.\n",
      "{'neg': 0.0, 'neu': 0.4, 'pos': 0.6, 'compound': 0.7184}\n",
      "It is complex yet intriguing.\n",
      "{'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}\n"
     ]
    }
   ],
   "source": [
    "# 情感分析\n",
    "sia = SentimentIntensityAnalyzer()\n",
    "for sentence in sentences:\n",
    "    print(sentence)\n",
    "    print(sia.polarity_scores(sentence))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "375d408e-24b7-4711-868a-42cc469973d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "原始文本: Natural Language Processing (NLP) is fascinating. It is complex yet intriguing.\n",
      "\n",
      "分词结果: ['Natural', 'Language', 'Processing', '(', 'NLP', ')', 'is', 'fascinating', '.', 'It', 'is', 'complex', 'yet', 'intriguing', '.']\n",
      "\n",
      "去除停用词后: ['Natural', 'Language', 'Processing', '(', 'NLP', ')', 'fascinating', '.', 'complex', 'yet', 'intriguing', '.']\n",
      "\n",
      "词性标注: [('Natural', 'JJ'), ('Language', 'NNP'), ('Processing', 'NNP'), ('(', '('), ('NLP', 'NNP'), (')', ')'), ('fascinating', 'NN'), ('.', '.'), ('complex', 'JJ'), ('yet', 'RB'), ('intriguing', 'VBG'), ('.', '.')]\n"
     ]
    }
   ],
   "source": [
    "# 输出结果\n",
    "print(\"\\n原始文本:\", text)\n",
    "print(\"\\n分词结果:\", words)\n",
    "print(\"\\n去除停用词后:\", filter_words)\n",
    "print(\"\\n词性标注:\", tagged_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45451ed7-e1f0-4585-80e9-3d9b616616b7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.3"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
